/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.Service
*        文件名:             UserService.svc.cs
*        创建系统时间:       2012/11/2 19:07:06
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.Service
{
    using System;
    using System.Data;
    using System.Reflection;
    using System.ServiceModel;
    using ECP.DataAccess;
    using ECP.DataAccess.Utilities;
    using ECP.Interface;
    using ECP.Model;
    using ECP.Utilities;

    [ServiceBehavior(Namespace = "http://jiangguang.net.cn/")]
    public partial class UserService : MarshalByRefObject, IUserService
    {
        private static UserService instance = null;
        private static object locker = new object();

        public string AddUser(BaseUserInfo userInfo, string company, string department, string workgroup, string userName, string realName, string code, string roleID, string password, string email, bool enabled, string description, out string statusCode, out string statusMessage)
        {
            string str = string.Empty;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                str = this.AddUser(dbHelper, userInfo, company, department, workgroup, userName, realName, code, roleID, password, email, enabled, description, out statusCode, out statusMessage);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return str;
        }

        public string AddUser(IDbHelper dbHelper, BaseUserInfo userInfo, string company, string department, string workgroup, string userName, string realName, string code, string role, string password, string email, bool enabled, string description, out string statusCode, out string statusMessage)
        {
            string str = string.Empty;
            BaseUserDao dao = new BaseUserDao(dbHelper, userInfo);
            str = dao.AddUser(company, department, workgroup, userName, realName, code, role, password, email, enabled, description, out statusCode);
            statusMessage = dao.GetStateMessage(statusCode);
            if ((!enabled && statusCode.Equals(StatusCode.OKAdd.ToString())) && !userInfo.ID.Equals(DefaultRole.Administrator.ToString()))
            {
                BaseMessageEntity messageEntity = new BaseMessageEntity();
                BaseMessageDao dao2 = new BaseMessageDao(dbHelper, userInfo);
                messageEntity.FunctionID = MessageFunction.WaitForAudit.ToString();
                messageEntity.ReceiverID = DefaultRole.Administrator.ToString();
                messageEntity.Content = userInfo.Realname + " 申请帐号：" + realName + "请审核。";
                str = dao2.Add(messageEntity);
            }
            return str;
        }

        public int AddUserToRole(BaseUserInfo userInfo, string userID, string[] addRoleIDs)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseUserRoleDao dao = new BaseUserRoleDao(dbHelper, userInfo);
                if (addRoleIDs != null)
                {
                    num += dao.AddToRole(userID, addRoleIDs);
                }
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int BatchDelete(BaseUserInfo userInfo, string[] ids)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseUserDao dao = new BaseUserDao(dbHelper, userInfo);
                num = dao.BatchDelete(ids);
                dao.CheckUserStaff();
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int Delete(BaseUserInfo userInfo, string id)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseUserDao dao = new BaseUserDao(dbHelper, userInfo);
                num = dao.Delete(id);
                dao.CheckUserStaff();
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public DataTable Get(BaseUserInfo userInfo, string id)
        {
            DataTable table = new DataTable(BaseUserTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                table = new BaseUserDao(dbHelper, userInfo).Get(id);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return table;
        }

        public DataTable GetDT(BaseUserInfo userInfo)
        {
            DataTable dT = new DataTable(BaseStaffTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                dT = new BaseUserDao(dbHelper, userInfo).GetDT();
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return dT;
        }

        public DataTable GetDTByIDs(BaseUserInfo userInfo, string[] ids)
        {
            DataTable table = new DataTable(BaseStaffTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                table = new BaseUserDao(dbHelper, userInfo).GetDT(BaseUserTable.FieldID, (object[])ids, BaseUserTable.FieldSortCode);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return table;
        }

        public DataTable GetRole(BaseUserInfo userInfo)
        {
            DataTable dataTable = new DataTable(BaseRoleTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                dataTable = new BaseRoleDao(dbHelper, userInfo).GetDT();
                BaseBusinessLogic.SetFilter(dataTable, BaseRoleTable.FieldEnabled, "1");
                if (!userInfo.IsAdministrator)
                {
                    foreach (DataRow row in dataTable.Rows)
                    {
                        if (row[BaseRoleTable.FieldID].ToString().ToUpper().Equals("Administrators"))
                        {
                            row.Delete();
                        }
                    }
                    dataTable.AcceptChanges();
                }
                dataTable.DefaultView.Sort = BaseUserTable.FieldSortCode;
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return dataTable;
        }

        public string[] GetUserRoleIDs(BaseUserInfo userInfo, string userID)
        {
            string[] roleIDs = null;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                roleIDs = new BaseUserRoleDao(dbHelper, userInfo).GetRoleIDs(userID);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return roleIDs;
        }

        public void Load()
        {
        }

        public int RemoveUserFromRole(BaseUserInfo userInfo, string userID, string[] removeRoleIDs)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseUserRoleDao dao = new BaseUserRoleDao(dbHelper, userInfo);
                if (removeRoleIDs != null)
                {
                    num += dao.RemoveFormRole(userID, removeRoleIDs);
                }
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public DataTable Search(BaseUserInfo userInfo, string search, string auditStates, string role)
        {
            DataTable table = new DataTable(BaseUserTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                table = new BaseUserDao(dbHelper, userInfo).Search(search, auditStates, role);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return table;
        }

        public int SetDefaultRole(BaseUserInfo userInfo, string userID, string roleID)
        {
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                new BaseUserDao(dbHelper, userInfo).SetProperty(userID, BaseUserTable.FieldRole, roleID);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return 0;
        }

        public int SetUserAuditStates(BaseUserInfo userInfo, string[] ids, AuditStatus auditStates)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseUserDao dao = new BaseUserDao(dbHelper, userInfo);
                num = dao.SetProperty(ids, BaseUserTable.FieldAuditStatus, auditStates);
                if (auditStates == AuditStatus.AuditPass)
                {
                    num = dao.SetProperty(ids, BaseUserTable.FieldEnabled, 1);
                }
                if (auditStates == AuditStatus.AuditReject)
                {
                    num = dao.SetProperty(ids, BaseUserTable.FieldEnabled, 0);
                }
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int UpdateUser(BaseUserInfo userInfo, string id, string userName, string fullName, string code, string role, bool enabled, out string statusCode, out string statusMessage)
        {
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseUserDao dao = new BaseUserDao(dbHelper, userInfo);
                num = dao.UpdateUser(id, userName, fullName, code, role, enabled, out statusCode);
                statusMessage = dao.GetStateMessage(statusCode);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public static UserService Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                        {
                            instance = new UserService();
                        }
                    }
                }
                return instance;
            }
        }

        public string HelloMessage(string userName)
        {
            return userName + "您好!\r\n" + "当前时间:" + System.DateTime.Now;
        }
    }
}